package aau.group52.tilbud.buddy;


import aau.group52.tilbud.buddy.actions.DrawerListener;
import aau.group52.tilbud.buddy.fragments.BaseListFragment;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.app.FragmentTransaction;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	private String[] leftCatList;
	private ListView leftDrawerList;
	private ActionBar bar;
	private DrawerLayout drawerLayout;
	private Resources res;
	private Fragment currentFragmentLoaded,searchPanel;
	private FragmentTransaction fTrans;
	private ActionBarDrawerToggle drawerListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// init all Views

		init();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.bar_menu, menu);

		return true;
	}
	/**
	 * Method to initialize resources of the app and main gui.
	 */
	private void init() {
		res = getResources();		
		bar = getActionBar();       
		// loading resources from res folder, string.xml, main.xml
		leftCatList = res.getStringArray(R.array.settingsList);
		leftDrawerList = (ListView) findViewById(R.id.left_drawer);
		drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		//Setting up drawer listener
		drawerListener=new DrawerListener(this, drawerLayout, R.drawable.ic_action_storage, R.string.param1, R.string.param2);
		bar.setDisplayHomeAsUpEnabled(true);
        drawerLayout.setDrawerListener(drawerListener);
		// Loading string array defined in /values/strings.xml and passing item
		// layout from Layout/leftitemlayout.xml
		leftDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.rightitemlayout, leftCatList));
		bar.show();
		// Loading fragment into activity
		currentFragmentLoaded = new BaseListFragment();
		fTrans = getFragmentManager().beginTransaction();
		fTrans.add(R.id.frgmCont, currentFragmentLoaded);	
		fTrans.commit();
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (drawerListener.onOptionsItemSelected(item)) {		
	          return true;
	        }
		switch (item.getItemId()) {
		case R.id.action_favorits:
			
			bar.setTitle(item.getTitle());
			break;
		case R.id.action_search:
			
			bar.setTitle(item.getTitle());
			
			break;
		case R.id.action_accounts:
			
			bar.setTitle(item.getTitle());
			break;
		case R.id.action_settings:
			
			bar.setTitle(item.getTitle());
			break;
		case R.id.action_about:
			
			bar.setTitle(item.getTitle());
			break;
		
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		drawerListener.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		drawerListener.syncState();
	}
    private class DrawerListListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
    	
    }
}
